package com.black.platform.permission.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.black.common.annotation.DataScope;
import com.black.platform.permission.po.Permission;
import com.black.common.vo.PermissionVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface PermissionMapper extends BaseMapper<Permission> {

    /**
     * 获取操作用户的所有权限
     *
     * @param userId           用户ID
     * @param systemCode       系统代码
     * @param isIncludeInvalid 是否包含无效的
     * @return 权限集合
     */
    @Select("<script>" +
            "SELECT p.permission_code permissionCode,p.permission_type permissionType,p.menu_id menuId FROM permission p,role_permission rp,position_role pr,user_position up " +
            "WHERE p.id= rp.permission_id AND rp.role_id = pr.role_id AND pr.position_id = up.position_id AND p.permission_type!='data' AND " +
            "up.user_id =#{ userId } AND p.system_code =#{ systemCode }" +
            "<when test='!isIncludeInvalid'>" +
            "AND p.valid ='1' " +
            "</when>" +
            "</script>")
    List<PermissionVo> getAllPermissionsByUser(@Param("userId") Long userId, @Param("systemCode") String systemCode, @Param("isIncludeInvalid") boolean isIncludeInvalid);

    /**
     * 获取操作用户的所有权限
     *
     * @param userId           用户ID
     * @param systemCode       系统代码
     * @param isIncludeInvalid 是否包含无效的
     * @return 权限集合
     */
    @Select("<script>" +
            "SELECT p.permission_code permissionCode,p.permission_type permissionType FROM permission p,role_permission rp,position_role pr,user_position up " +
            "WHERE p.id= rp.permission_id AND rp.role_id = pr.role_id AND pr.position_id = up.position_id AND p.permission_type='data' AND " +
            "up.user_id =#{ userId } AND p.system_code =#{ systemCode }" +
            "<when test='!isIncludeInvalid'>" +
            "AND p.valid ='1' " +
            "</when>" +
            "</script>")
    List<PermissionVo> getAllDataPermissionsByUser(@Param("userId") Long userId, @Param("systemCode") String systemCode, @Param("isIncludeInvalid") boolean isIncludeInvalid);

    @Select("<script>" +
            "SELECT pm.* " +
            "FROM role_permission rp,permission pm " +
            "WHERE rp.role_code = #{roleCode} AND " +
            "rp.permission_code = pm.permission_code AND pm.system_code = #{systemCode}" +
            "AND pm.status ='1'" +
            "<when test='!isIncludeInvalid'>" +
            "AND rp.status ='1' " +
            "</when>" +
            "</script>")
    List<Permission> getAllPermissionsByRoleCode(@Param("roleCode") String roleCode, @Param("systemCode") String systemCode, @Param("isIncludeInvalid") boolean isIncludeInvalid);
}
